package com.brilliance.project.common.tools;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;

import com.brilliance.project.webapp.form.sysmanage.AgentForm;

/**
 * 内存工厂，提供了访问内存，查找，组装memoryObject等的方法
 * 
 * @author HLJ
 * 
 */
public class MemoryFactory {
	// 放查询的对象Key
	public static String MEMORY_KEY = "MEMORY_KEY";
	// 放用户登录状态Key
	public static String LOGIN_KEY = "LOGIN_KEY";

	/**
	 * 把object放到内存中去
	 * 
	 * @param memObject
	 * @param context
	 */
	public static void putMemoryObject(String key, MemoryObject memObject,
			ServletContext context) {
		Map map = null;
		Object obj = context.getAttribute(MEMORY_KEY);
		if (null == obj) {
			map = new HashMap<String, Object>();
			memObject.setCreatTime(new Date());
			map.put(key, memObject);
			context.setAttribute(MEMORY_KEY, map);
		} else {
			map = (Map) obj;
			// 设置放入资料的时间
			memObject.setCreatTime(new Date());
			map.put(key, memObject);
		}
	}

	/**
	 * 从内存中找到指定key的object
	 * 
	 * @param key
	 * @param context
	 */
	public static MemoryObject getMemoryObject(String key, long durSecond,
			ServletContext context) {
		// System.out.println("传入进来的值："+durSecond);
		// System.out.println(0==durSecond);
		if (0 == durSecond) {
			// System.out.println("2");
			// 假如是限制时间是0，则直接返回null
			return null;
		}

		Object obj = context.getAttribute(MEMORY_KEY);
		if (null == obj) {
			return null;
		}
		Map map = (Map) obj;
		Object memoryObject = map.get(key);
		if (null == memoryObject) {
			return null;
		}
		// 假如找到，判读是否过期
		MemoryObject mo = (MemoryObject) memoryObject;
		if (MemoryFactory.isOverdue(mo.getCreatTime(), durSecond)) {
			return null;
		}
		return mo;
	}

	/**
	 * 返回当前时间和建立此资料的时间差 已秒计
	 * 
	 * @return
	 */
	public static long getDurSecond(Date date) {
		return (new Date().getTime() - date.getTime()) / 1000;
	}

	/**
	 * 判断是否过期
	 * 
	 * @param durSecond
	 *            过期的期限，以秒计
	 * @return
	 */
	public static boolean isOverdue(Date date, long durSecond) {
		if (durSecond > getDurSecond(date)) {
			return false;
		}
		return true;
	}

	// ######################################################################
	/**
	 * 判断用户是否已经登录
	 */
	public static boolean isExistAgent(AgentForm agentForm,
			ServletContext context) {
		Map<String, AgentForm> map = null;
		Object obj = context.getAttribute(LOGIN_KEY);
		if (null == obj) {
			// 1. memory中没有任何用户，重新new HashMap
			map = new HashMap<String, AgentForm>();
			agentForm.setLoginTime(new Date());
			map.put(agentForm.getAgent_id(), agentForm);
			context.setAttribute(LOGIN_KEY, map);
			return false;
		} else {
			// 2. memory中已有保存用户的对象
			map = (Map<String, AgentForm>) obj;
			Object afObj = map.get(agentForm.getAgent_id());
			if (null == afObj) {
				// 2.1 找不到此agentId，此用户没有登陆过
				agentForm.setLoginTime(new Date());
				map.put(agentForm.getAgent_id(), agentForm);
				return false;
			} else {
				// 2.2 找到此agentId用户
				AgentForm af = (AgentForm) afObj;
				if (agentForm.getSessionId().equals(af.getSessionId())) {
					// 2.2.1 seesionId相同，是同一登录用户
					return false;
				} else {
					// 2.2.2 sessionId不同，用户在其他地方已经登录
					return true;
				}
			}
		}
	}
}
